﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;
using HYElectricKernel;

namespace HYRElectric.LightningCalculation.Forms
{
	public partial class LevelListForm : System.Windows.Forms.Form
	{
		public LevelListForm(Document doc, List<Level> existingLevels)
		{
			this.m_Doc = doc;
			this.m_ExistingLevels = existingLevels;
			this.InitializeComponent();
		}

		public List<Level> GetAddLevels()
		{
			List<Level> list = new List<Level>();
			foreach (object obj in this.LevelListView.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				if (listViewItem.Checked)
				{
					list.Add(listViewItem.Tag as Level);
				}
			}
			return list;
		}

		private void LevelListForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.InitializeLevelListView();
			this.LoadDlgData();
		}

		private void LevelListForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveDlgData();
		}

		private void SaveDlgData()
		{
			string workingLocation = Product.WorkingLocation;
			string xmlPath = Path.Combine(workingLocation, "LevelListForm.xml");
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(workingLocation, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			xmlHelper.SetValue("xPos", base.Location.X.ToString());
			xmlHelper.SetValue("yPos", base.Location.Y.ToString());
			xmlHelper.SaveXml(xmlPath);
		}

		private void LoadDlgData()
		{
			string text = Path.Combine(Product.WorkingLocation, "LevelListForm.xml");
			if (!File.Exists(text))
			{
				return;
			}
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(text, "Root");
			xmlHelper.SelectNode("Root\\Datas");
			int x = Convert.ToInt32(xmlHelper.GetValue("xPos"));
			int y = Convert.ToInt32(xmlHelper.GetValue("yPos"));
            base.Location = new System.Drawing.Point(x, y);
		}

		private void InitializeLevelListView()
		{
			this.LevelListView.BeginUpdate();
			using (List<Level>.Enumerator enumerator = Common.GetSortLevels(this.m_Doc, 0.0, double.MaxValue).GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Level level = enumerator.Current;
					double val = Common.FeetToMM(level.Elevation) / 1000.0;
					if (!Geometry.Lessthan_Or_Equal(val, 0.0) && this.m_ExistingLevels.FindIndex((Level value) => Geometry.IsEqual(value.Elevation, level.Elevation, 0.01)) == -1)
					{
						ListViewItem listViewItem = new ListViewItem();
						listViewItem.Text = "";
						listViewItem.Tag = level;
						listViewItem.SubItems.Add(level.Name);
						listViewItem.SubItems.Add(val.ToString("f0"));
						this.LevelListView.Items.Add(listViewItem);
					}
				}
			}
			this.LevelListView.EndUpdate();
		}

		private List<Level> m_ExistingLevels;

		private Document m_Doc;
	}
}
